package com.eurake.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eurake.seckill.Config.RedisConfig;
import com.eurake.seckill.Exception.GlobalException;
import com.eurake.seckill.mapper.UserMapper;
import com.eurake.seckill.pojo.User;
import com.eurake.seckill.service.IUserService;
import com.eurake.seckill.utils.CookieUtils;
import com.eurake.seckill.utils.MD5Util;
import com.eurake.seckill.utils.UUIDUtil;
import com.eurake.seckill.utils.ValidatorUtil;
import com.eurake.seckill.vo.LoginVo;
import com.eurake.seckill.vo.RespBean;
import com.eurake.seckill.vo.RespBeanEnum;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chen
 * @since 2024-11-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisTemplate<String,User> redisTemplate;

    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
//        if(mobile==null || password==null){
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//        if(!ValidatorUtil.isMobile(mobile)){
//            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
//        }
        //根据手机号获取用户
        User user = userMapper.selectById(mobile);
        if(user==null){
            throw new GlobalException(RespBeanEnum.USER_NOT_FIND);
        }
        //判断密码是否正确
        if(!(MD5Util.transPassToDBPass(password,user.getSlat()).equals(user.getPassword()))){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        //生成cookie
        String token = UUIDUtil.uuid();
//        request.getSession().setAttribute(token,user);
        redisTemplate.opsForValue().set("user:"+token,user);
        CookieUtils.setCookie(request,response,"token",token);
        return RespBean.success(token);
    }
    public User getUserByToken(String token,HttpServletRequest request,HttpServletResponse response) {
        if(token==null){
            return null;
        }
        User user = (User)redisTemplate.opsForValue().get("user:"+token);
        if(user!=null){
            CookieUtils.setCookie(request,response,"token",token);
        }
        return user;
    }

    @Override
    public RespBean updatePassword(String token, String newPassword, HttpServletRequest request, HttpServletResponse response) {
        User user = getUserByToken(token,request,response);
        if(user==null){
            throw new GlobalException(RespBeanEnum.USER_NOT_FIND);
        }
        user.setPassword(MD5Util.transPassToDBPass(newPassword,user.getSlat()));
        int result = userMapper.updateById(user);
        if(result==1){
            //更新成功，删除session
            redisTemplate.delete("user:"+token);
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);
    }
}
